package com.song.controller.admin;

import com.aliyuncs.exceptions.ClientException;
import com.song.constant.MessageConstant;
import com.song.result.Result;
import com.song.utils.AliyunOssUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    private AliyunOssUtil aliyunOssUtil;
    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file)  {
        String url = "";
     try {
         url = aliyunOssUtil.upload(file);
     }
     catch (ClientException | IOException e) {
         log.info("上传失败:{}", e.getMessage());
         return Result.error(MessageConstant.UPLOAD_FAILED);
     }
     return Result.success(url, "上传成功");
    }
}
